package net.bitacademy.java22.domain;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

//SQLiteOpenHelper를 상속받아 생성자를 생성, calendarList.db가 DAO와 연결될 DB다. 
public class EventDAO extends SQLiteOpenHelper {
	SQLiteDatabase db;
	ContentValues row;
	Context context;
	
	ProfileDAO p = new ProfileDAO(context);
	
	public EventDAO(Context context) {
		super(context, "pascal.db", null, 1);
		this.context = context;
	}	
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS  calendarlist (id TEXT PRIMARY KEY ASC , title TEXT, start TEXT, end TEXT, tag TEXT, FOREIGN KEY(tag) REFERENCES hardware(tag) )");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS calendarlist");
		onCreate(db);
	}
	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		db.execSQL("CREATE TABLE IF NOT EXISTS  calendarlist (id TEXT PRIMARY KEY ASC , title TEXT, start TEXT, end TEXT, tag TEXT, FOREIGN KEY(tag) REFERENCES hardware(tag) )");    
	}
	
	// id값을 넘겨주면 hwsetting 값을 넘겨준다
	public void getHwsetting(String iid) {
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT tag FROM calendarlist WHERE id=\""+iid+"\"", null);
		ProfileVO pvo  ;
		while (cursor.moveToNext()) {
			String tag = cursor.getString(0);
			db = this.getReadableDatabase();
			cursor = db.rawQuery("SELECT hwsetting FROM hardware WHERE tag=\""+tag+"\"", null);
			while (cursor.moveToNext()) {
				pvo = new ProfileVO();
				pvo.setHwSetting(cursor.getInt(0));
			}
		}
	}
	
	// id값을 넘겨주면 hwsetting 값을 넘겨준다
	public ProfileVO getHwsetting2(String iid){
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT hardware.hwsetting FROM calendarlist INNER JOIN hardware ON calendarlist.tag=hardware.tag AND calendarlist.id=\""+iid+"\"" , null);
		ProfileVO pvo= new ProfileVO();
		while (cursor.moveToNext()) {
			pvo.setHwSetting(cursor.getInt(0));
	}

		Log.d("핸드폰세팅","OK");
		return pvo;
	}
	
	public ArrayList<EventVO> statusList(){
		db = this.getReadableDatabase();
		EventVO vo = null;
		Cursor cursor;
		cursor = db.rawQuery("SELECT id, tag, start,end FROM calendarlist", null);
		ArrayList<EventVO> result =new ArrayList<EventVO>();
		while (cursor.moveToNext()) {
			vo = new EventVO();
			vo.setId(cursor.getString(0));
			vo.setTag(cursor.getString(1));
			vo.setStart(cursor.getString(2));
			vo.setEnd(cursor.getString(3));
			result.add(vo);
		}
			cursor.close();
			db.close();
			return result;
	}

	public void insert(EventVO evo) {
		db = this.getWritableDatabase();
		row = new ContentValues();
		
		row.put("id", evo.getId());
		row.put("title", evo.getTitle());
		row.put("start", evo.getStart());
		row.put("end", evo.getEnd());
		row.put("tag", evo.getTag());
				
		db.insert("calendarlist", null, row);
		this.close();
	}
	public void delete() {
		db = this.getWritableDatabase();
		db.delete("calendarlist", null, null);
		this.close();	
	}
	public String select() {
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM calendarlist", null);
		String result = "";
		while (cursor.moveToNext()) {
			String id = cursor.getString(0);
			String title = cursor.getString(1);
			String start = cursor.getString(2);
			String end = cursor.getString(3);
			String tag = cursor.getString(4);
			result += (id + " , " + title + " , " + start + " , " + end + " , " + tag + "\n");
		}
		if (result.length() == 0) {
			return "Empyt Set";
		} else {
			return result;
		}
	}
	
	public boolean exist() {
		boolean ret = false;
		db = this.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM calendarlist", null);
		if(cursor.getCount() == 0){
			ret = false;
		} else{
			ret = true;
		}
		return ret;
	}	
}	


